package Domain;

import java.util.Observer;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * The domain controller
 * 
 * @author kenneth
 */
public class DomainController {
    
    private static DomainController instance;
    private DropboxServer dropboxServer;
    private final Serverlog serverlog;
    
    private DomainController() {
        serverlog = new Serverlog();
    }
    
    /**
     * Singleton pattern
     *
     * @return The domaincontroller instance
     */
    public static DomainController getDomainController() {
        if(instance == null) {
            instance = new DomainController();
        }
        return instance;
    }
    
    /**
     * Adds an observer to the serverlog observable.
     * 
     * @param observer The observer
     */
    public void addServerlogObserver(Observer observer) {
        serverlog.addObserver(observer);
    }
    
     /**
     * Creates a new dropbox server instance and starts it.
     */
    public void startServer() {      
        dropboxServer = new DropboxServer(serverlog); 
        ExecutorService threadExecutor = Executors.newFixedThreadPool(1);
        threadExecutor.execute(dropboxServer);
    }
     
    /**
     * Stops the dropbox server.
     */
    public void stopServer() {
        dropboxServer.stopServer();
    }
    
    /**
     * Asks the dropbox server whether it is running or not.
     *
     * @return True when the server is running.
     */
    public boolean serverIsRunning() {
        return dropboxServer.isRunning();
    }
    
}
